package com.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.util.LogUtil;
import com.util.R;

import java.util.ArrayList;
import java.util.List;


public class SplashActivity extends BaseActivity {
	private SharedPreferences pre;
	private SharedPreferences.Editor editor;
	private List<View> viewList;
	private Handler handlerSplash;
    private Handler handlerMain;
	private Runnable runnableSplash;
	private Runnable runnableMain;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
        initData();

	}

	/**
	 * 设置启动模式为SingleTop时，若再次启动同一个Activity会调用该方法
	 * @param intent
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		LogUtil.i("MyTestAdapter","onNewIntent");
		editor=pre.edit();
		editor.putBoolean("isFirstUse",false);
		editor.commit();
		initViewPager();
	}

	@Override
	protected void initData() {
		super.initData();
		pre = getSharedPreferences("launchInfo", MODE_PRIVATE);
		boolean isFirstUse=pre.getBoolean("isFirstUse",true);
		if(isFirstUse){ //��һ��ʹ��
			handlerSplash=new Handler();
			runnableSplash=new Runnable() {
				@Override
				public void run() {
					Intent it = new Intent(SplashActivity.this, SplashActivity.class);
					startActivity(it);
				}
			};
			handlerSplash.postDelayed(runnableSplash, 2500);

		}else{
			handlerMain=new Handler();
			runnableMain=new Runnable() {
				@Override
				public void run() {
					Intent it = new Intent(SplashActivity.this, MainActivity.class);
					startActivity(it);
					finish();
				}
			};
			handlerMain.postDelayed(runnableMain, 2500);

		}

	}



	/**
	 * 初始化ViewPager
	 */
	private void initViewPager() {
		viewList = new ArrayList<>();
		ViewPager vp = (ViewPager) findViewById(R.id.id_viewpager);
		vp.setVisibility(View.VISIBLE);

		View view1 = new View(SplashActivity.this);
		view1.setBackgroundResource(R.mipmap.bg_guide1);
		View view2 = new View(SplashActivity.this);
		view2.setBackgroundResource(R.mipmap.bg_guide2);
		View view3 = new View(SplashActivity.this);
		view3.setBackgroundResource(R.mipmap.bg_guide3);
		View view4 = LayoutInflater.from(SplashActivity.this).inflate(R.layout.splash04,null);


		View v_click = view4.findViewById(R.id.v_click_to_enter);
		v_click.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent(SplashActivity.this, MainActivity.class);
				startActivity(it);
				finish();
			}
		});
		viewList.add(view1);
		viewList.add(view2);
		viewList.add(view3);
		viewList.add(view4);
		PagerAdapter mAdapter = new PagerAdapter() {

			@Override
			public void destroyItem(ViewGroup container, int position,
									Object object) {
				container.removeView(viewList.get(position));
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				View view = viewList.get(position);
				container.addView(view);
				return view;
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return viewList.size();
			}
		};
		vp.setAdapter(mAdapter);
	}

	/**
	 * 捕获回退键，取消相关操作
	 * @param keyCode
	 * @param event
	 * @return
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (handlerSplash != null) {
				if(runnableSplash!=null){
					handlerSplash.removeCallbacks(runnableSplash);
				}
			}
			if (handlerMain != null) {
				if(runnableMain!=null){
					handlerMain.removeCallbacks(runnableMain);
				}
			}
			finish();
		}
		return true;
	}

}
